package com.ruoyi.bbsAdmin.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bbsAdmin.domain.Plate;
import com.ruoyi.bbsAdmin.domain.UserSchool;
import com.ruoyi.bbsAdmin.mapper.PlateMapper;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bbsAdmin.mapper.SchoolMapper;
import com.ruoyi.bbsAdmin.domain.School;
import com.ruoyi.bbsAdmin.service.ISchoolService;

/**
 * 学校Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-04-11
 */
@Service
public class SchoolServiceImpl implements ISchoolService 
{
    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private PlateMapper plateMapper;

    /**
     * 查询学校
     * 
     * @param schoolid 学校主键
     * @return 学校
     */
    @Override
    public School selectSchoolBySchoolid(Long schoolid)
    {
        return schoolMapper.selectSchoolBySchoolid(schoolid);
    }

    /**
     * 查询学校列表
     * 
     * @param school 学校
     * @return 学校
     */
    @Override
    public List<School> selectSchoolList(School school)
    {
        return schoolMapper.selectSchoolList(school);
    }

    /**
     * 根據權限查询学校列表
     *
     * @param school 学校
     * @return 学校
     */
    @Override
    public List<School> selectSchoolListByRole(School school) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysRole> roles = user.getRoles();
        Set<String> rolestrs = new HashSet<>();
        for (SysRole r : roles
        ) {
            rolestrs.add(r.getRoleKey());
        }
        if (rolestrs.contains("admin")) {
            return schoolMapper.selectSchoolList(school);
        } else {
            // 存在多个校园，sql
            List<String> schoolIds = new ArrayList<>();
            for (String rolestr : rolestrs
            ) {
                if (rolestr.contains("xygly")) {
                    String schoolid = rolestr.replace("xygly", "");
                    if (!"".equals(schoolid)) {
                        schoolIds.add(rolestr.replace("xygly", ""));
                    }

                }
            }
            return schoolMapper.selectSchoolListByRole(school, schoolIds);
        }
    }

    /**
     * 新增学校
     * 
     * @param school 学校
     * @return 结果
     */
    @Override
    public int insertSchool(School school)
    {
        schoolMapper.insertSchool(school);
        //获取第一个学校下的所有官方板块并创建
        QueryWrapper<Plate> plateQueryWrapper = new QueryWrapper<>();
        plateQueryWrapper.eq("schoolId",1);
        plateQueryWrapper.ne("cateId",3);
        List<Plate> officialPlates = plateMapper.selectList(plateQueryWrapper);
        for (int i = 0; i < officialPlates.size(); i++) {
            Plate plate = new Plate();
            plate.setSchoolid(school.getSchoolid());
            plate.setCateid(officialPlates.get(i).getCateid());
            plate.setUserid("official");
            plate.setName(officialPlates.get(i).getName());
            plate.setIntroduction(officialPlates.get(i).getIntroduction());
            plate.setPortraiturl(officialPlates.get(i).getPortraiturl());
            plate.setBackimgurl(officialPlates.get(i).getBackimgurl());
            plateMapper.insertPlate(plate);
        }
        return 1;
    }

    /**
     * 修改学校
     * 
     * @param school 学校
     * @return 结果
     */
    @Override
    public int updateSchool(School school)
    {
        return schoolMapper.updateSchool(school);
    }

    /**
     * 批量删除学校
     * 
     * @param schoolids 需要删除的学校主键
     * @return 结果
     */
    @Override
    public int deleteSchoolBySchoolids(Long[] schoolids)
    {
        return schoolMapper.deleteSchoolBySchoolids(schoolids);
    }

    /**
     * 删除学校信息
     * 
     * @param schoolid 学校主键
     * @return 结果
     */
    @Override
    public int deleteSchoolBySchoolid(Long schoolid)
    {
        return schoolMapper.deleteSchoolBySchoolid(schoolid);
    }
}
